package com.sdk.pay.transfer.api.params;

import android.annotation.SuppressLint;

import com.sdk.pay.transfer.utils.LogUtil;
import com.sdk.pay.transfer.constant.PayContext;
import com.sdk.pay.transfer.utils.PayDESUtil;
import com.sdk.pay.transfer.utils.PayMD5Util;

import org.json.JSONException;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/5/4.
 */

public class DESParams extends Params {

    @Override
    public String toString() {
        return null;
    }

    @Override
    public RequestBody getRequestBody() {
        @SuppressLint("SimpleDateFormat")
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String nonce = PayMD5Util.getMD5(dateFormat.format(new Date()));

        String str = null;
        try {
            str = mObject.getJSONArray(PayContext.UPLOAD_LIST_KEY).toString();
        } catch (JSONException e) {
            e.printStackTrace();
            str = mObject.toString();
        }
        String replace = str.replace("\\/", "/");
        LogUtil.log("mObject.toString() = " + replace);

        String encodeMsg = PayDESUtil.encode(nonce, replace);
        LogUtil.log("PayDESUtil.nonce = " + nonce);
        LogUtil.log("PayDESUtil.encode = " + encodeMsg);
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("nonce", nonce);
        builder.add("msg", encodeMsg);
        return builder.build();
    }

    @Override
    public RequestBody getRequestBodyNoEncryption() {
        String str = mObject.toString();
        LogUtil.log("mObject.toString() = " + str);

        FormBody.Builder builder = new FormBody.Builder();
        Iterator it = mObject.keys();
        while (it.hasNext()) {
            String key = (String) it.next();
            try {
                builder.add(key, mObject.getString(key));
            } catch (JSONException e) {
                e.printStackTrace();
                break;
            }
        }
        return builder.build();
    }
}
